package cn.itcast.service.impl;

import cn.itcast.dto.OrdersDto;
import cn.itcast.entity.*;
import cn.itcast.mapper.*;
import cn.itcast.service.IOrderService;
import cn.itcast.util.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IAddressMapper addressMapper;
    @Autowired
    private IShoppingCartMapper shoppingCartMapper;
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IOrderDetailsMapper orderDetailsMapper;



    @Override
    public void submit(Orders orders, Long userId) {
        //根据userId查询user表
        User user = userMapper.findByUserId(userId);
        //根据userId查询shoppingCart
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(userId);
        //根据addressBookId查询addressBook
        AddressBook addressBook = addressMapper.getById(orders.getAddressBookId());
        //UUID生成OrderId
        long orderId = UUIDUtils.getUUIDInOrderId().longValue();
        //遍历购物车列表,将购物车信息复制到orderDetail
        //orders与orderDetails是一对多关系 准备集合接收封装好的orderDetails
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        //准备接受总金额的变量
        BigDecimal totalAmount = BigDecimal.ZERO;
        //准备接受总数量的变量
        Long number = 0l;
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            //UUID生成orderDetailID
            long orderDetailId = UUIDUtils.getUUIDInOrderId().longValue();
            orderDetail.setId(orderDetailId);
            orderDetail.setOrderId(orderId);
            orderDetails.add(orderDetail);
            //设置总金额以及总数量，为封装order准备
            totalAmount=totalAmount.add(shoppingCart.getAmount().multiply(BigDecimal.valueOf(shoppingCart.getNumber())));
            number+=shoppingCart.getNumber();
        }
        //向order_detail插入数据
        orderDetailsMapper.saveBatch(orderDetails);

        //封装Order
        //前台已经传入 addressBookId、payMethod、remark
        orders.setId(orderId);
        orders.setUserId(userId);
        orders.setNumber(String.valueOf(number));
        orders.setStatus(user.getStatus());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now().plusMinutes(30));
        orders.setAmount(totalAmount);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        //向order插入数据
        orderMapper.save(orders);

    }

    @Override
    public Page<Orders> page(Integer pageNum, Integer pageSize, String number, String beginTime, String endTime) {
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> orders = orderMapper.page(number, beginTime, endTime);
        PageInfo<Orders> pageInfo = new PageInfo<>(orders);
        return new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageSize,pageNum);
    }

    @Override
    public Page<OrdersDto> userPage(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrdersDto> ordersDto = orderMapper.userPage(userId);
        PageInfo<OrdersDto> pageInfo = new PageInfo<>(ordersDto);

        return new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageSize,pageNum);
    }
}
